# -*- coding: utf-8 -*-

import psycopg2

from pymongo import MongoClient

ENV_TEST = True


class Configure(object):
    PG_SHARDS = {
        'host': "192.168.200.12",
        'password': 'BDi92hFG',
        'port': '5439',
        'database': 'sneaky',
        'user': 'sneaky'
    }

    if ENV_TEST:
        PG_SHARDS = {
            'host': "172.16.10.162",
            'password': '77WN88wwc',
            'port': '5432',
            'database': 'sneaky',
            'user': 'sneaky'
        }
        MONGO_URL = 'mongodb://172.16.10.130:22000/'


class Postgres(object):

    def __init__(self):
        self.conn = psycopg2.connect(**Configure.PG_SHARDS)

    def close(self):
        self.conn.close()


pg = Postgres()
mongo = MongoClient(Configure.MONGO_URL)


class Song(object):

    @classmethod
    def insert(cls, name, artist, uid, origin_url, lyric, next_lyric):
        song_sql = "insert into lark_song(name,artist) values('%s','%s') returning id" % (name, artist)
        section_sql = "insert into lark_song_section(song_id) values(%s) returning id"
        audio_sql = "insert into lark_guide_audio (uid, section_id,origin_url) values (%s,%s,'%s')"
        conn = pg.conn
        cur = conn.cursor()
        try:
            cur.execute(song_sql)
            song_id = int(cur.fetchone()[0])
            cur.execute(section_sql % song_id)
            section_id = int(cur.fetchone()[0])
            cur.execute(audio_sql % (uid, section_id, origin_url))
            pg.conn.commit()
        except Exception:
            conn.rollback()
        finally:
            cur.close()
        with mongo.sneaky.lark_lyric as cur:
            _id = 'lark_%s_%s' % (song_id, section_id)
            cur.insert(dict(_id=_id, lyric=lyric, next_lyric=next_lyric))


l1 = ["lark_102541_104816", "lark_102366_104817", "lark_103446_104818", "lark_102494_104819", "lark_102670_104823",
      "lark_102567_104824", "lark_102993_104825", "lark_102469_104827", "lark_102547_104828", "lark_103107_104836",
      "lark_103315_104840", "lark_102922_104841", "lark_103637_104842", "lark_102735_104846", "lark_103827_104849",
      "lark_103668_104850", "lark_102893_104851", "lark_103624_104852", "lark_102918_104853", "lark_103080_104854",
      "lark_103309_104857", "lark_102667_104858", "lark_102334_104859", "lark_102955_104864", "lark_102747_104865",
      "lark_103888_104869", "lark_102518_104871", "lark_102944_104882", "lark_102623_104884", "lark_103228_104889",
      "lark_102625_104890", "lark_103246_104891", "lark_102503_104892", "lark_102615_104894", "lark_103572_104896",
      "lark_102917_104899", "lark_103830_104901", "lark_102539_104903", "lark_102799_104904", "lark_103062_104906",
      "lark_103739_104907", "lark_102553_104909", "lark_102969_104911", "lark_102815_104913", "lark_103350_104915",
      "lark_103365_104916", "lark_103351_104921", "lark_103075_104923", "lark_103132_104926", "lark_103185_104928",
      "lark_102795_104929", "lark_103390_104933", "lark_103186_104934", "lark_102782_104936", "lark_103662_104937",
      "lark_103379_104938", "lark_102608_104940", "lark_102968_104942", "lark_102702_104943", "lark_102930_104945",
      "lark_103425_104950", "lark_102938_104952", "lark_103582_104953", "lark_102629_104956", "lark_103271_104960",
      "lark_103360_104964", "lark_103398_104970", "lark_103801_104971", "lark_103105_104972", "lark_103131_104974",
      "lark_103427_104975", "lark_103434_104976", "lark_103693_104982", "lark_103521_104983", "lark_102613_104988",
      "lark_103273_104989", "lark_103790_104993", "lark_102561_104996", "lark_103226_104997", "lark_102666_104999",
      "lark_103491_105004", "lark_102371_105005", "lark_102342_105007", "lark_103533_105008", "lark_103102_105011",
      "lark_103319_105012", "lark_102512_105013", "lark_103326_105014", "lark_102336_105016", "lark_103503_105018",
      "lark_102473_105019", "lark_102982_105021", "lark_103866_105022", "lark_102391_105023", "lark_102734_105026",
      "lark_103041_105032", "lark_102361_105033", "lark_102898_105035", "lark_102496_105039", "lark_103805_105046",
      "lark_102721_105047", "lark_103607_105052", "lark_102427_105057", "lark_102762_105060", "lark_102365_105062",
      "lark_102497_105065", "lark_103196_105066", "lark_102346_105071", "lark_103833_105073", "lark_102614_105075",
      "lark_102711_105076", "lark_102679_105077", "lark_103735_105079", "lark_103239_105081", "lark_103399_105082",
      "lark_103432_105086", "lark_102429_105087", "lark_102580_105088", "lark_103762_105089", "lark_102401_105091",
      "lark_103396_105093", "lark_103466_105094", "lark_102872_105106", "lark_102533_105107", "lark_103792_105111",
      "lark_103312_105112", "lark_103736_105113", "lark_102726_105114", "lark_102337_105116", "lark_103346_105122",
      "lark_102400_105128", "lark_103195_105129", "lark_103708_105131", "lark_102714_105134", "lark_103802_105135",
      "lark_102787_105136", "lark_103538_105137", "lark_102516_105138", "lark_103151_105142", "lark_102860_105143",
      "lark_103261_105146", "lark_102487_105147", "lark_103620_105148", "lark_103300_105149", "lark_103867_105150",
      "lark_103716_105153", "lark_102563_105155", "lark_102376_105157", "lark_103206_105159", "lark_102432_105161",
      "lark_102326_105162", "lark_103555_105163", "lark_102991_105166", "lark_102997_105168", "lark_103661_105171",
      "lark_103514_105174", "lark_102437_105179", "lark_102801_105181", "lark_103626_105182", "lark_103516_105183",
      "lark_102450_105186", "lark_102618_105187", "lark_103197_105189", "lark_103890_105190", "lark_103537_105191",
      "lark_103583_105194", "lark_103634_105195", "lark_102718_105198", "lark_103426_105200", "lark_102685_105206",
      "lark_103201_105207", "lark_102348_105208", "lark_103719_105213", "lark_103571_105215", "lark_102676_105218",
      "lark_103176_105219", "lark_102947_105221", "lark_103264_105223", "lark_103641_105227", "lark_103345_105232",
      "lark_103387_105238", "lark_102495_105244", "lark_102607_105246", "lark_102565_105249", "lark_102994_105252",
      "lark_102839_105253", "lark_103421_105254", "lark_103394_105255", "lark_103663_105257", "lark_103318_105260",
      "lark_102862_105268", "lark_103636_105271", "lark_102472_105272", "lark_102999_105280", "lark_102591_105281",
      "lark_102958_105283", "lark_102904_105286", "lark_103072_105288", "lark_102456_105289", "lark_103327_105290",
      "lark_103548_105300", "lark_102384_105307", "lark_103819_105314", "lark_102946_105315", "lark_103612_105316",
      "lark_102587_105317", "lark_103710_105325", "lark_103189_105326", "lark_102620_105327", "lark_103191_105329",
      "lark_103811_105332", "lark_103611_105333", "lark_102636_105335", "lark_102355_105336", "lark_103324_105337",
      "lark_102634_105339", "lark_103887_105340", "lark_103417_105344", "lark_103386_105345", "lark_103384_105347",
      "lark_103687_105355", "lark_102886_105356", "lark_102865_105359", "lark_103597_105360", "lark_102980_105361",
      "lark_103748_105363", "lark_103284_105365", "lark_102425_105368", "lark_102476_105369", "lark_103834_105370",
      "lark_102770_105373", "lark_102807_105375", "lark_103865_105377", "lark_103489_105378", "lark_103575_105379",
      "lark_103323_105380", "lark_102545_105381", "lark_102759_105382", "lark_102833_105384", "lark_102579_105386",
      "lark_102395_105387", "lark_102531_105391", "lark_102510_105394", "lark_102878_105399", "lark_103854_105404",
      "lark_103065_105405", "lark_102834_105406", "lark_103027_105407", "lark_103325_105408", "lark_103700_105410",
      "lark_103587_105411", "lark_103139_105412", "lark_103872_105416", "lark_103095_105417", "lark_102647_105421",
      "lark_103047_105422", "lark_102514_105425", "lark_103125_105427", "lark_102392_105429", "lark_103788_105432",
      "lark_102970_105433", "lark_102644_105434", "lark_103786_105439", "lark_103580_105440", "lark_102645_105442",
      "lark_103757_105445", "lark_102320_105450", "lark_103578_105452", "lark_102689_105453", "lark_102542_105454",
      "lark_103839_105456", "lark_102895_105458", "lark_103117_105459", "lark_103272_105461", "lark_103749_105463",
      "lark_103416_105465", "lark_103100_105466", "lark_102771_105467", "lark_103588_105468", "lark_102424_105471",
      "lark_103800_105472", "lark_103378_105475", "lark_102686_105478", "lark_102707_105481", "lark_102504_105482",
      "lark_103870_105483", "lark_102540_105484", "lark_103873_105486", "lark_102652_105487", "lark_102728_105488",
      "lark_103752_105490", "lark_102315_105493", "lark_103500_105496", "lark_103212_105497", "lark_103311_105499",
      "lark_103793_105502", "lark_102345_105503", "lark_103280_105506", "lark_102656_105508", "lark_102616_105509",
      "lark_102651_105510", "lark_103308_105513", "lark_103340_105517", "lark_102883_105518", "lark_103154_105522",
      "lark_103670_105524", "lark_103161_105525", "lark_103746_105529", "lark_102927_105530", "lark_103646_105532",
      "lark_103331_105533", "lark_102687_105534", "lark_102525_105535", "lark_102975_105542", "lark_103469_105544",
      "lark_103130_105545", "lark_102330_105547", "lark_102329_105548", "lark_102853_105549", "lark_102340_105555",
      "lark_102877_105556", "lark_102660_105557", "lark_102835_105562", "lark_103842_105563", "lark_103030_105566",
      "lark_102359_105567", "lark_102744_105570", "lark_103078_105574", "lark_103688_105575", "lark_102352_105577",
      "lark_103173_105578", "lark_103121_105583", "lark_102880_105584", "lark_102571_105585", "lark_103773_105587",
      "lark_102937_105588", "lark_102823_105590", "lark_102407_105591", "lark_103561_105593", "lark_103344_105594",
      "lark_102534_105595", "lark_103287_105596", "lark_103262_105597", "lark_103094_105598", "lark_102414_105599",
      "lark_102419_105600", "lark_103806_105607", "lark_103743_105609", "lark_102402_105613", "lark_102856_105614",
      "lark_102455_105619", "lark_103795_105620", "lark_103601_105621", "lark_102523_105623", "lark_102362_105625",
      "lark_103574_105626", "lark_102859_105630", "lark_102452_105633", "lark_103393_105634", "lark_102430_105636",
      "lark_103799_105638", "lark_103825_105641", "lark_103317_105642", "lark_103007_105643", "lark_103814_105646",
      "lark_103508_105649", "lark_102731_105654", "lark_102775_105655", "lark_102675_105657", "lark_103257_105663",
      "lark_102631_105669", "lark_102741_105670", "lark_103328_105672", "lark_103478_105674", "lark_102698_105676",
      "lark_103831_105679", "lark_102948_105682", "lark_103342_105687", "lark_103553_105689", "lark_103630_105690",
      "lark_103110_105692", "lark_102492_105695", "lark_102777_105696", "lark_102916_105698", "lark_102736_105702",
      "lark_102626_105705", "lark_102578_105706", "lark_103178_105708", "lark_102925_105711", "lark_102816_105723",
      "lark_103159_105728", "lark_102845_105732", "lark_102530_105735", "lark_103471_105737", "lark_103796_105738",
      "lark_103089_105742", "lark_102901_105743", "lark_103657_105744", "lark_103717_105746", "lark_102486_105747",
      "lark_102867_105748", "lark_103879_105750", "lark_102808_105751", "lark_103335_105753", "lark_103712_105759",
      "lark_102858_105760", "lark_102851_105766", "lark_102661_105767", "lark_102378_105769", "lark_102617_105770",
      "lark_102421_105771", "lark_102749_105773", "lark_102463_105780", "lark_103794_105784", "lark_102390_105785",
      "lark_102527_105789", "lark_102742_105794", "lark_103614_105797", "lark_102964_105798", "lark_103177_105802",
      "lark_103604_105803", "lark_102511_105806", "lark_103433_105808", "lark_102434_105810", "lark_103824_105811",
      "lark_102976_105812", "lark_103861_105813", "lark_102908_105814", "lark_102973_105815", "lark_103347_105818",
      "lark_102727_105819", "lark_102992_105823", "lark_103084_105824", "lark_103845_105825", "lark_103495_105828",
      "lark_103771_105829", "lark_102590_105830", "lark_102758_105832", "lark_102646_105833", "lark_103744_105837",
      "lark_103775_105838", "lark_102572_105839", "lark_103424_105840", "lark_103724_105841", "lark_102370_105842",
      "lark_102448_105844", "lark_103456_105848", "lark_103314_105852", "lark_103829_105853", "lark_103560_105854",
      "lark_103036_105855", "lark_103286_105860", "lark_103120_105863", "lark_102408_105867", "lark_103810_105868",
      "lark_103519_105869", "lark_102570_105872", "lark_102855_105873", "lark_102940_105875", "lark_103552_105877",
      "lark_103435_105880", "lark_102358_105883", "lark_102716_105890", "lark_103375_105892", "lark_103413_105895",
      "lark_102719_105896", "lark_102761_105897", "lark_103509_105898", "lark_103499_105899", "lark_102803_105900",
      "lark_103729_105901", "lark_103758_105902", "lark_103705_105904", "lark_103207_105912", "lark_103366_105921",
      "lark_102987_105922", "lark_102825_105925", "lark_103187_105928", "lark_103769_105929", "lark_103512_105934",
      "lark_102505_105936", "lark_103878_105938", "lark_102522_105940", "lark_102896_105942", "lark_103353_105943",
      "lark_103307_105946", "lark_102907_105953", "lark_103077_105956", "lark_102850_105957", "lark_102715_105960",
      "lark_103442_105961", "lark_102374_105964", "lark_103774_105965", "lark_103215_105967", "lark_103418_105970",
      "lark_103647_105972", "lark_103021_105975", "lark_102341_105977", "lark_103826_105979", "lark_102331_105980",
      "lark_103550_105981", "lark_103043_105982", "lark_103511_105984", "lark_102446_105985", "lark_102451_105986",
      "lark_103675_105988", "lark_102324_105992", "lark_103200_105994", "lark_103543_105995", "lark_103149_105997",
      "lark_103639_106002", "lark_103832_106005", "lark_103638_106010", "lark_102659_106012", "lark_102632_106013",
      "lark_103817_106014", "lark_103163_106015", "lark_103681_106020", "lark_103392_106027", "lark_103431_106029",
      "lark_103490_106030", "lark_103779_106032", "lark_102673_106034", "lark_103461_106035", "lark_103617_106037",
      "lark_103863_106040", "lark_102849_106046", "lark_103745_106050", "lark_103497_106053", "lark_102776_106055",
      "lark_103589_106060", "lark_103868_106062", "lark_102598_106063", "lark_102389_106064", "lark_103787_106066",
      "lark_103734_106069", "lark_102924_106070", "lark_102412_106071", "lark_103510_106072", "lark_102443_106075",
      "lark_103409_106076", "lark_103680_106077", "lark_102671_106079", "lark_102566_106080", "lark_103122_106081",
      "lark_103840_106084", "lark_102864_106092", "lark_103452_106094", "lark_102612_106095", "lark_102624_106096",
      "lark_103474_106098", "lark_102515_106099", "lark_102805_106101", "lark_103703_106102", "lark_102453_106103",
      "lark_102335_106105", "lark_103836_106111", "lark_103013_106112", "lark_102319_106116", "lark_103828_106117",
      "lark_103621_106118", "lark_102630_106120", "lark_102332_106126", "lark_102538_106127", "lark_103502_106130",
      "lark_103255_106140", "lark_102692_106141", "lark_103485_106142", "lark_103332_106143", "lark_102988_106147",
      "lark_102688_106149", "lark_103823_106151", "lark_102664_106157", "lark_103756_106158", "lark_103522_106159",
      "lark_103454_106164", "lark_103465_106166", "lark_103846_106167", "lark_103635_106170", "lark_102756_106172",
      "lark_103049_106174", "lark_102870_106176", "lark_102879_106182", "lark_102483_106184", "lark_103738_106186",
      "lark_102466_106187", "lark_103025_106188", "lark_103874_106189", "lark_102681_106190", "lark_103316_106204",
      "lark_103241_106206", "lark_103003_106207", "lark_103171_106208", "lark_102409_106212", "lark_103798_106216",
      "lark_102781_106217", "lark_102537_106223", "lark_103737_106224", "lark_103883_106227", "lark_102363_106228",
      "lark_103770_106229", "lark_102635_106230", "lark_103448_106236", "lark_102852_106240", "lark_102353_106242",
      "lark_103172_106244", "lark_102513_106246", "lark_103494_106249", "lark_102551_106251", "lark_103547_106253",
      "lark_102454_106254", "lark_102831_106257", "lark_103605_106258", "lark_103408_106260", "lark_102339_106261",
      "lark_102601_106262", "lark_103024_106263", "lark_103889_106265", "lark_103640_106266", "lark_103838_106267",
      "lark_103696_106270", "lark_103216_106274", "lark_102411_106275", "lark_102995_106276", "lark_102977_106277",
      "lark_102385_106278", "lark_103664_106283", "lark_103111_106284", "lark_103763_106285", "lark_103293_106288",
      "lark_102507_106295", "lark_103142_106296", "lark_103765_106299", "lark_103470_106300", "lark_102956_106303",
      "lark_103048_106305", "lark_103619_106306", "lark_103415_106307", "lark_102405_106311", "lark_103244_106314",
      "lark_103815_106318", "lark_103665_106320", "lark_103856_106325", "lark_102972_106326", "lark_102748_106327",
      "lark_102383_106330", "lark_103014_106332", "lark_102750_106334", "lark_102464_106335", "lark_103337_106336",
      "lark_103835_106338", "lark_103382_106339", "lark_102573_106343", "lark_102501_106346", "lark_102404_106347",
      "lark_103263_106349", "lark_103152_106353", "lark_103701_106354", "lark_102584_106356", "lark_103669_106360",
      "lark_103613_106361", "lark_103422_106368", "lark_103034_106370", "lark_103524_106371", "lark_102774_106376",
      "lark_103081_106378", "lark_102778_106379", "lark_102939_106381", "lark_102343_106384", "lark_102900_106387",
      "lark_103292_106389", "lark_103289_106390", "lark_103058_106396", "lark_103742_106398", "lark_103213_106399",
      "lark_102648_106402"]


def main():
    # Song.insert('七里香', '周杰伦', 991, '991/assadaq2')
    with open('20190122_lyric_next_lyric_new_id.txt', 'r') as f:
        for line in f.readlines():
            params = line.split('\t')
            _id = 'lark_%s_%s' % (params[0], params[1])
            lyric, next_lyric = params[-2], params[-1]
            if _id in l1:
                mongo.sneaky.lark_lyric.insert(dict(_id=_id, lyric=lyric, next_lyric=next_lyric))


if __name__ == '__main__':
    main()
